/*
 * Copyright 2011 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.impetus.teamplan.mb;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.impetus.teamplan.Constants;
import com.impetus.teamplan.PlanMonth;
import com.impetus.teamplan.dao.TeamPlanDao;
import com.impetus.teamplan.dao.TeamPlanDaoImpl;
import com.impetus.teamplan.entity.Accomplishment;
import com.impetus.teamplan.entity.Member;
import com.impetus.teamplan.entity.Responsibility;
import com.impetus.teamplan.web.TeamPlanUtils;
import com.impetus.teamplan.web.WebUtils;

/**
 * @author amresh.singh
 *
 */
public class MemberBean {
	
	private String memberId;
	private String memberName;
	private String responsibilitiesStr;
	private String accomplishmentStr;
	private String nextMonth;
	private String nextYear;
	private String monthStr;	
	
	/**
	 * @return the memberId
	 */
	public String getMemberId() {
		return memberId;
	}
	/**
	 * @param memberId the memberId to set
	 */
	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}
	/**
	 * @return the memberName
	 */
	public String getMemberName() {
		return memberName;
	}
	/**
	 * @param memberName the memberName to set
	 */
	public void setMemberName(String memberName) {
		this.memberName = memberName;
	}
	/**
	 * @return the responsibilitiesStr
	 */
	public String getResponsibilitiesStr() {
		return responsibilitiesStr;
	}
	/**
	 * @param responsibilitiesStr the responsibilitiesStr to set
	 */
	public void setResponsibilitiesStr(String responsibilitiesStr) {
		this.responsibilitiesStr = responsibilitiesStr;
	}	

	/**
	 * @return the accomplishmentStr
	 */
	public String getAccomplishmentStr() {
		return accomplishmentStr;
	}
	/**
	 * @param accomplishmentStr the accomplishmentStr to set
	 */
	public void setAccomplishmentStr(String accomplishmentStr) {
		this.accomplishmentStr = accomplishmentStr;
	}
	/**
	 * @return the nextMonth
	 */
	public String getNextMonth() {
		return nextMonth;
	}
	/**
	 * @param nextMonth the nextMonth to set
	 */
	public void setNextMonth(String nextMonth) {
		this.nextMonth = nextMonth;
	}
	/**
	 * @return the nextYear
	 */
	public String getNextYear() {
		return nextYear;
	}
	/**
	 * @param nextYear the nextYear to set
	 */
	public void setNextYear(String nextYear) {
		this.nextYear = nextYear;
	}
	
	/**
	 * @return the monthStr
	 */
	public String getMonthStr() {
		return monthStr;
	}
	/**
	 * @param monthStr the monthStr to set
	 */
	public void setMonthStr(String monthStr) {
		this.monthStr = monthStr;
	}
	public String addMember() {
		System.out.println("Adding Member:" + getMemberId() + "-" + getMemberName() + "/" + getResponsibilitiesStr());
		Member memberToBeAdded = new Member();
		memberToBeAdded.setMemberId(getMemberId());
		memberToBeAdded.setMemberName(getMemberName());
		memberToBeAdded.setPassword(Constants.DEFAULT_PASSWORD);
		memberToBeAdded.setMemberType(Constants.MEMBER_TYPE_TEAM_MEMBER);
		List<String> responsibilityStrings = WebUtils.getDataFromTextArea(getResponsibilitiesStr());
		List<Responsibility> responsibilities = new ArrayList<Responsibility>();
		for(String s : responsibilityStrings) {
			Responsibility r = new Responsibility();
			r.setResponsibilityId(WebUtils.getUniqueId());
			r.setResponsibilityDescription(s);
			
			responsibilities.add(r);
		}
		
		memberToBeAdded.setResponsibilities(responsibilities);
		
		//Persist in database
		TeamPlanDao dao = new TeamPlanDaoImpl();
		dao.addOrUpdateMember(memberToBeAdded);
		
		//Update list in session
		TeamPlanBean teamPlanBean = (TeamPlanBean)WebUtils.getManagedBeanFromSession("teamPlanBean");
		teamPlanBean.getMembers().add(memberToBeAdded);
		WebUtils.setManagedBeanInSession("teamPlanBean", teamPlanBean);
		
		return "memberAdded";
		
	}
	
	
	public String editMember() {
		System.out.println("Editting Member:" + getMemberId() + "-" + getMemberName() + "/" + getResponsibilitiesStr());
		
		String memberToBeEditted = getMemberId();	
		TeamPlanBean teamPlanBean = (TeamPlanBean)WebUtils.getManagedBeanFromSession("teamPlanBean");
		//Updating session		
		List<Member> allMembers = new ArrayList<Member>();
		Member memberObjToPersist = null;
		for(Member member : teamPlanBean.getMembers()) {
			if(memberToBeEditted.equals(member.getMemberId())) {
				member.setMemberName(getMemberName());
				List<Responsibility> responsibilities = new ArrayList<Responsibility>();
				for(String responsibility : WebUtils.getDataFromTextArea(getResponsibilitiesStr())) {
					responsibilities.add(new Responsibility(WebUtils.getUniqueId(), responsibility));
				}
				member.setResponsibilities(responsibilities);
				allMembers.add(member);
				memberObjToPersist = member;
				
			} else {
				allMembers.add(member);
			}
		}
		teamPlanBean.setMembers(allMembers);
		WebUtils.setManagedBeanInSession("teamPlanBean", teamPlanBean);
		
		//Persist in DB
		TeamPlanDao dao = new TeamPlanDaoImpl();	
		dao.addOrUpdateMember(memberObjToPersist);		
		System.out.println("Member " + memberObjToPersist + " was successfully updated");		
		return "memberUpdated";		
	}
	
	public String deleteMember() {		
		String memberToBeDeleted = getMemberId();
		System.out.println("Deleting Member " + memberToBeDeleted);
		
		TeamPlanDao dao = new TeamPlanDaoImpl();
		
		
		
		//Updating session
		TeamPlanBean teamPlanBean = (TeamPlanBean)WebUtils.getManagedBeanFromSession("teamPlanBean");		
		List<Member> allMembers = new ArrayList<Member>();		
		for(Member member : teamPlanBean.getMembers()) {
			if(!memberToBeDeleted.equals(member.getMemberId())) {
				allMembers.add(member);
			} else {
				dao.deleteMember(member);
			}
		}
		
		teamPlanBean.setMembers(allMembers);
		WebUtils.setManagedBeanInSession("teamPlanBean", teamPlanBean);
		return "memberDeteled";
		
	}
	
	public String addMonthlyPlan() {
		System.out.println("Adding monthly plan for Member:" + getMemberId() + "-" + getMemberName() + "/" + getAccomplishmentStr());
		
		String memberToBeEditted = getMemberId();	
		TeamPlanBean teamPlanBean = (TeamPlanBean)WebUtils.getManagedBeanFromSession("teamPlanBean");
		//Updating session		
		List<Member> allMembers = new ArrayList<Member>();
		Member memberObjToPersist = null;
		for(Member member : teamPlanBean.getMembers()) {
			if(memberToBeEditted.equals(member.getMemberId())) {				
				List<Accomplishment> accomplishments = member.getAccomplishments();
				
				accomplishments.add(new Accomplishment(WebUtils.getUniqueId(), WebUtils.removeLeadingZero(getNextMonth()), getNextYear(), WebUtils.getDataFromTextArea(getAccomplishmentStr())));
				member.setAccomplishments(accomplishments);
				
				allMembers.add(member);
				memberObjToPersist = member;
				
			} else {
				allMembers.add(member);
			}
		}
		teamPlanBean.setMembers(allMembers);
		WebUtils.setManagedBeanInSession("teamPlanBean", teamPlanBean);
		
		//Persist in DB
		TeamPlanDao dao = new TeamPlanDaoImpl();	
		dao.addOrUpdateMember(memberObjToPersist);		
		System.out.println("Member " + memberObjToPersist + " was successfully updated");		
		return "Monthly plan added";		
	}
	
	
	public void setMemberBeanInSession(ActionEvent e) {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String[]> values = context.getExternalContext().getRequestParameterValuesMap();
		String memberId = values.get("memberId")[0];
		System.out.println("Member to be modified is: " + memberId);		
		MemberBean memberBean = (MemberBean)WebUtils.getManagedBeanFromSession("memberBean");
		TeamPlanBean teamPlanBean = (TeamPlanBean)WebUtils.getManagedBeanFromSession("teamPlanBean");
		for(Member member : teamPlanBean.getMembers()) {
			if(memberId.equals(member.getMemberId())) {
				System.out.println("Setting parameters for Member " + member.getMemberId() + "to be modified");
				memberBean.setMemberId(member.getMemberId());
				memberBean.setMemberName(member.getMemberName());
				
				List<String> resStr = new ArrayList<String>();
				for(Responsibility res : member.getResponsibilities()) {
					resStr.add(res.getResponsibilityDescription());
				}
				memberBean.setResponsibilitiesStr(WebUtils.createDataForTextArea(resStr));
				PlanMonth pm = TeamPlanUtils.getNextMonthForPlanning(member);
				memberBean.setNextMonth(pm.getMonth());
				memberBean.setNextYear(pm.getYear());
				memberBean.setMonthStr(WebUtils.getMonth(pm.getMonth()));
				
				System.out.println("Parameters set " + memberBean.getMemberId() + "-" + memberBean.getMemberName() + "/" + memberBean.getResponsibilitiesStr() + "PM" + memberBean.getNextMonth() + "'" + memberBean.getNextYear());				
				WebUtils.setManagedBeanInSession("memberBean", memberBean);
				break;
			}
		}	
	}

}
